package com.bright.cwduijie.duijie.util;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @Author hxj
 * @Date 2023/12/27 14:18
 * @Description 列表工具类
 */
public class ListUtil {

    /**
     * steam.map.collect
     * @param list
     * @param function
     * @return
     * @param <T>
     * @param <R>
     */
    public static <T,R> List<T> toSingleList(List<R> list, Function<R, T> function) {
        return list.stream().map(function).collect(Collectors.toList());
    }

    /**
     * stream.filter.count
     * @param list
     * @param predicate
     * @return
     * @param <R>
     */
    public static <R> long countByFilter(List<R> list, Predicate<? super R> predicate) {
        return list.stream().filter(predicate).count();
    }

    /**
     * stream.filter.collect
     * @param list
     * @param predicate
     * @return
     * @param <T>
     */
    public static <T> List<T> toListByFilter(List<T> list, Predicate<? super T> predicate) {
        return list.stream().filter(predicate).collect(Collectors.toList());
    }

    /**
     * 找出列表中所有重复数据
     * @param list
     * @return
     * @param <T>
     */
    public static <T> List<T> findRepeatedData(List<T> list) {
        return list.stream()
                .distinct()
                .filter(obj -> Collections.frequency(list, obj) > 1)
                .collect(Collectors.toList());
    }

    /**
     * int[]转List<Integer>
     * @param intArray
     * @return
     */
    public static List<Integer> toIntegerListByArray(int[] intArray) {
        return Arrays.stream(intArray).boxed().collect(Collectors.toList());
    }


    /**
     * list取子集合 （toIndex可以取超过列表长度的下标）
     * @param list
     * @param fromIndex
     * @param toIndex
     * @return
     * @param <T>
     */
    public static <T> List<T> getSubList(List<T> list, int fromIndex, int toIndex) {
        return list.subList(fromIndex, Math.min(toIndex, list.size()));
    }

    public static <T> Set<T> getSubSet(List<T> list, int fromIndex, int toIndex) {
        List<T> ts = list.subList(fromIndex, Math.min(toIndex, list.size()));
        return new HashSet<>(ts);
    }


    public static <E> boolean isEmpty(Collection<E> collection) {
        return collection == null || collection.isEmpty();
    }

    public static <K, V> boolean isEmpty(Map<K, V> map) {
        return map == null || map.isEmpty();
    }

    public static <E> boolean isNotEmpty(Collection<E> collection) {
        return collection != null && !collection.isEmpty();
    }

    public static <K, V> boolean isNotEmpty(Map<K, V> map) {
        return map != null && !map.isEmpty();
    }

    /*
     * 低版本jdk环境下 对jdk1.8 stream.map功能的替代
     *  List<String> fileItemCollect = fileItems.stream()
     *                 .map(fileItem -> fileItem.getFieldName() + " as " + fileItem.getFieldName())
     *                 .collect(Collectors.toList());
     */
//    public static <T, R> List<T> toSingleList2(List<R> list, ObjectConverter<T, R> converter) {
//        List<T> result = new ArrayList<>();
//
//        for (R object : list) {
//            T convertResult = converter.convert(object);
//            result.add(convertResult);
//        }
//
////        list.stream().map(converter).collect(Collectors.toList());
//
//        return result;
//    }



//    public static <T> List<T> jsonArrayToSingleList(JSONArray jsonArray, ObjectConverter<T, JSONObject> converter) {
//        List<T> result = new ArrayList<>();
//
//        for (int i = 0; i < jsonArray.size(); i++) {
//            T convertResult = converter.convert(jsonArray.getJSONObject(i));
//            result.add(convertResult);
//        }
//
//        return result;
//    }

//    public interface ObjectConverter<T, R> {
//        T convert(R object);
//    }

}
